package asdf.springmvc.controller;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import org.springframework.session.web.http.HttpSessionManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lichengwen
 * @since 2016/4/1 14:10
 */
@Controller
@RequestMapping("/hello")
public class HelloWorldController {

    private static final Logger LGR = LoggerFactory.getLogger(HelloWorldController.class);

    @Autowired
    private SessionRepository sessionRepository;

    @RequestMapping("/say/{name}")
    @ResponseBody
    public String sayHello(
            @PathVariable("name") String name) {
        LGR.info("saying hello to " + name);
        return "hello " + name + ".";
    }

    @RequestMapping("/sayHelloWorld")
    @ResponseBody
    public HelloWorld sayHelloWorld(HttpServletRequest request) {
        HttpSessionManager hsm = (HttpSessionManager)
                request.getAttribute(HttpSessionManager.class.getName());
        LGR.info("HttpSessionManager => " + hsm);
        LGR.info("saying hello world.");
        return new HelloWorld();
    }

    @Data
    public static class HelloWorld {
        private String hello = "hello";
        private String world = "world";
    }

}
